package mediatheque.view;

import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.LayoutStyle;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListSelectionModel;



import mediatheque.model.MediathequeModel;

public class GestionDocumentsView extends ListeDocumentsView{
	

	private static final long serialVersionUID = 4072711192699103265L;
	private JButton boutonAjout;
	private JButton boutonModification;
	private JButton boutonSuppression;
	
	
	public GestionDocumentsView(MediathequeModel model) {
		super(model);
	}
	
	@Override
	protected void initialisationComposants(){
		
		Font fontBouton = new Font("Verdana", 0, 12);		
		boutonAjout = new JButton("Ajouter");
		boutonAjout.setFont(fontBouton);	
		boutonModification= new JButton("Modifier");
		boutonModification.setFont(fontBouton);	
		boutonModification.setEnabled(false);
		boutonSuppression = new JButton("Supprimer");
		boutonSuppression.setFont(fontBouton);
		boutonSuppression.setEnabled(false);
		
		super.initialisationComposants();
		
		tableauDocuments.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting())return;			
				boolean afficher = !((ListSelectionModel)e.getSource()).isSelectionEmpty();
				boutonModification.setEnabled(afficher);
		        boutonSuppression.setEnabled(afficher);
			}
		});
	}
	
	@Override
	protected void placerComposants() {
		GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
        		layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(289, 289, 289)
                    .addComponent(boutonAjout)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(boutonModification)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(boutonSuppression)
                    .addContainerGap(442, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
	                .addGap(38, 38, 38)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(32, 32, 32)
	                        .addComponent(lblNom))
	                    .addComponent(lblIdentifiant))
	                .addGap(18, 18, 18)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
	                    .addComponent(tfNom)
	                    .addComponent(tfIdentifiant, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
	                .addGap(40, 40, 40)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
	                    .addComponent(lblDescription)
	                    .addComponent(lblType))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
	                    .addComponent(cbType)
	                    .addComponent(tfDescription, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
	                .addGap(67, 67, 67)
	                .addComponent(lblDate)
	                .addGap(18, 18, 18)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(tfDate, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(boutonReset))
	                .addGap(346, 346, 346))
                .addComponent(scrollTableau, GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            		.addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(lblIdentifiant)
                                .addComponent(tfIdentifiant, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(lblNom)
                                .addComponent(tfNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblDescription)
                                .addComponent(tfDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblDate)
                                .addComponent(tfDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(lblType)
                                .addComponent(cbType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(boutonReset))))
                    .addGap(18, 18, 18)
                    .addComponent(scrollTableau, GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(boutonAjout)
                        .addComponent(boutonModification)
                        .addComponent(boutonSuppression))
                    .addContainerGap())
        );
        
	}


	public void addAjouterDocumentListener(ActionListener al){
		boutonAjout.addActionListener(al);
	}
	
	public void addSupprimerDocumentListener(ActionListener al){
		boutonSuppression.addActionListener(al);
	}
	
	public void addModifierDocumentListener(ActionListener al) {
		boutonModification.addActionListener(al);
	}

}
